package com.xuecheng.system.init;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xuecheng.common.enums.StatusAnnotation;
import com.xuecheng.system.entity.Dictionary;
import com.xuecheng.system.mapper.DictionaryMapper;
import org.reflections.Reflections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * Created on 2021/04/06/20:04 周二
 *
 * @author Lantin
 */

//@Component
public class StatusScan implements ApplicationRunner {

    @Autowired
    private DictionaryMapper mapper;

    /**
     * Callback used to run the bean.
     *
     * @param args incoming application arguments
     * @throws Exception on error
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {

        Reflections reflection = new Reflections("com.xuecheng.common.enums");

        System.out.println("springboot启动时执行了  StatusScan。。。。。");
        Set<Class<?>> typesAnnotatedWith = reflection.getTypesAnnotatedWith(StatusAnnotation.class);

        List<Dictionary> dicList = new ArrayList<>(Collections.emptyList());

        for (Class<?> aClass : typesAnnotatedWith) {

            StatusAnnotation annotation = aClass.getAnnotation(StatusAnnotation.class);
            String code = annotation.code();
            String name = annotation.name();
            Dictionary dictionary = new Dictionary();

            if (aClass.isEnum()) {
                String typeName = aClass.getTypeName();
                Class<Enum> clazz;
                try {
                    clazz = (Class<Enum>) Class.forName(typeName);
                    //添加get属性值的方法
                    Method getCode = clazz.getMethod("getCode");
                    Method getDesc = clazz.getMethod("getDesc");
                    Enum[] enumConstants = clazz.getEnumConstants();
                    JSONArray jsonArray = new JSONArray();
                    for (Enum enumConstant : enumConstants) {
                        System.out.println(enumConstant.name());
                        String fieldCode = (String) getCode.invoke(enumConstant);
                        String desc = (String) getDesc.invoke(enumConstant);
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("code", fieldCode);
                        jsonObject.put("desc", desc);
                        jsonArray.add(jsonObject);
                    }
                    String jsonString = JSONUtils.toJSONString(jsonArray);
                    dictionary.setItemValues(jsonString);
                } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
            dictionary.setCode(code);
            dictionary.setName(name);
            dicList.add(dictionary);
        }
        try {
            mapper.truncateTable();
            mapper.insertAll(dicList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String encodedPwd = "3f116f16b8b99557a9067cd5fc21d1849868dae8";

        System.out.println(encodedPwd.length());
        MessageDigest md5 = MessageDigest.getInstance("md5");
        //16进制字节数组
        byte[] digest = md5.digest("ymfe.org".getBytes(StandardCharsets.UTF_8));

        char hexDigits[] =
                {'0', '1', '2', '3',
                        '4', '5', '6', '7',
                        '8', '9', 'a', 'b',
                        'c', 'd', 'e', 'f'};

        int j = digest.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte b = digest[i];
            str[k++] = hexDigits[b >> 4 & 0xf];
            str[k++] = hexDigits[b & 0xf];
        }
        String result = new String(str);
        System.out.println(result);

    }

}

